package com.codingfuture.glacioralarm.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codingfuture.glacioralarm.dto.THDateDTO;
import com.codingfuture.glacioralarm.entity.AlarmRecord;
import com.codingfuture.glacioralarm.entity.Meter;
import com.codingfuture.glacioralarm.entity.WareHouse;
import com.codingfuture.glacioralarm.mapper.AlarmRecordMapper;
import com.codingfuture.glacioralarm.mapper.MetersMapper;
import com.codingfuture.glacioralarm.mapper.WareHouseMapper;
import com.codingfuture.glacioralarm.service.AlarmRecordService;
import org.apache.naming.factory.SendMailFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author :zxw
 * @date 2025-04-11 08:53
 * @description:
 */
@Service
public class AlarmRecordServiceImpl implements AlarmRecordService {
    @Autowired
    private MetersMapper metersMapper;
    @Autowired
    private WareHouseMapper wareHouseMapper;
    @Autowired
    private AlarmRecordMapper alarmRecordMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Cacheable("alarm_list")
    @CacheEvict(value = "alarm_record_date", allEntries = true)
    @Override
    public boolean selectMeter(THDateDTO thDateDTO) {

        if(thDateDTO==null){
            return false;
        }

//        QueryWrapper<THDateDTO> wrapper = new QueryWrapper<>();
        Meter meter = metersMapper.selectBySn(thDateDTO.getSn());
        boolean isHumWithinRange = thDateDTO.getHum() >= meter.getMinHum() && thDateDTO.getHum() <= meter.getMaxHum();

        // 检查温度是否在允许的范围内
        boolean isTemWithinRange = thDateDTO.getTem() >= meter.getMinTem() && thDateDTO.getTem() <= meter.getMaxTem();

//        System.err.println(isHumWithinRange && isTemWithinRange);
        if (isHumWithinRange && isTemWithinRange) {
            return false;
        }
        if (meter == null) {
            return false;
        }
        WareHouse wareHouse = wareHouseMapper.selectById(meter.getWarehouseId());
        AlarmRecord alarmRecord = new AlarmRecord();
        alarmRecord.setMeterSn(meter.getSn());
        alarmRecord.setMeterName(meter.getName());
        alarmRecord.setHostSn(meter.getHostSn());
        alarmRecord.setHostName(meter.getHostName());
        alarmRecord.setWarehouseName(meter.getWarehouseName());
        alarmRecord.setCompanyName(meter.getCompanyName());
        alarmRecord.setMeterId(meter.getId());
        alarmRecord.setHum(thDateDTO.getHum());
        alarmRecord.setMaxHum(meter.getMaxHum());
        alarmRecord.setMinHum(meter.getMinHum());
        alarmRecord.setTem(thDateDTO.getTem());
        alarmRecord.setMaxTem(meter.getMaxTem());
        alarmRecord.setMinTem(meter.getMinTem());
        alarmRecord.setStartTime(LocalDateTime.now());
        alarmRecord.setAlarmState(0);
        alarmRecord.setWarehouseSn(meter.getWarehouseSn());
        alarmRecord.setCreateTime(LocalDateTime.now());
        if(wareHouse !=null){
       alarmRecord.setWarehouseType(wareHouse.getType());
        }


        Integer temAlert = 0;
        Integer humAlert = 0;
        if (thDateDTO.getTem() < meter.getMinTem()) {
            temAlert = 1;
        }
        if (thDateDTO.getTem() > meter.getMaxTem()) {
            temAlert = 2;

        }

        if (thDateDTO.getHum() < meter.getMinHum()) {
            humAlert = 1;
        }
        if (thDateDTO.getHum() > meter.getMaxHum()) {
            humAlert = 2;
        }

        alarmRecord.setHumAlert(humAlert);
        alarmRecord.setTemAlert(temAlert);
        if(temAlert!=0&&humAlert==0){
            alarmRecord.setAlarmType(0);
        } else if (temAlert == 0&&humAlert != 0) {
            alarmRecord.setAlarmType(1);
        } else if (temAlert == 0 && humAlert == 0) {
            alarmRecord.setAlarmType(2);
        } else if (temAlert !=0  && humAlert !=0 ) {
            alarmRecord.setAlarmType(3);
        }
//        alarmRecord.setAlarmType((temAlert != 0 ? 1 : 0) + (humAlert != 0 ? 2 : 0));

        alarmRecordMapper.insert(alarmRecord);
        //发布消息
        String jsonResult = JSON.toJSONString(meter);//把结果类转化为JSON发布出去
        stringRedisTemplate.convertAndSend("alarmDate", jsonResult);

        return true;


    }


}
